<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="费用名称:">
        <el-input v-model="queryParams.frtName" size="small" placeholder="请输入费用名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" align="center" prop="customerName" width="180" />
      <el-table-column label="费用名称" align="center" prop="frtName" width="180" />
      <el-table-column label="费用类型" align="center" prop="chargeType" width="180" />
      <el-table-column label="结算基于" align="center" prop="settleBy" width="180" />
      <el-table-column label="结算时间单位" align="center" prop="chargeTimeBy" width="180" />
      <el-table-column label="金额/百分比" align="center" prop="amount" width="180" />
      <el-table-column label="入库费/出库费" align="center" prop="inAndOutCharge" width="180" />
      <el-table-column label="转换单位" align="center" prop="convertQuantity" width="180" />
      <el-table-column label="开始数量" align="center" prop="startQuantity" width="180" />
      <el-table-column label="最大数量" align="center" prop="endQuantity" />
      <el-table-column label="有效期开始日" align="center" prop="startDateFrom" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDateFrom, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期到" align="center" prop="validUntil" width="180" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed>
        <template slot-scope="scope">
          <el-button v-hasPermission="['pms:chargeConfig:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['pms:chargeConfig:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
   <el-dialog :title="chargeConfigFormDialog.title" :visible.sync="chargeConfigFormDialog.visible" :close-on-click-modal="false" :before-close="resetChargeConfigFormDialog" width="900px" append-to-body>
         <el-form ref="chargeConfigForm" :model="chargeConfigFormData" :rules="chargeConfigFormRules" label-width="100px">
            <el-row :gutter="24" class="form-item-group">
               <el-col :span="8">
           <el-form-item label="客户" prop="customerId">
             <el-input v-model="chargeConfigFormData.customerId" disabled />
           </el-form-item>
            </el-col>
             <el-col :span="8">
           <el-form-item label="费用编码" prop="frtNo">
             <el-select v-model="chargeConfigFormData.frtNo" style="width: 100%;" clearable>
               <el-option v-for="option in $constant.frtNoOptions" :key="option.key" :label="option.label" :value="option.key" />
             </el-select>
           </el-form-item>
           </el-col>
             <el-col :span="8">
           <el-form-item label="结算基于" prop="settleBy">
             <el-select v-model="chargeConfigFormData.settleBy" style="width: 100%;" clearable>
               <el-option v-for="option in $constant.settleByOptions" :key="option.key" :label="option.label" :value="option.key" />
             </el-select>
           </el-form-item>
           </el-col>
             </el-row>
        <el-row :gutter="24" class="form-item-group">
           <el-col  v-if="chargeConfigFormData.frtNo === '仓储费'" :span="8">
           <el-form-item label="结算时间单位" prop="chargeTimeBy">
             <el-radio-group v-model="chargeConfigFormData.chargeTimeBy">
               <el-radio v-for="option in $constant.chargeTimeByOptions" :key="option.key" :label="option.key">{{ option.label }}</el-radio>
             </el-radio-group>
           </el-form-item>
           </el-col>
             <el-col  :span="8">
           <el-form-item label="金额或百分比" prop="amount">
             <el-input v-model="chargeConfigFormData.amount" placeholder="请输入金额或百分比" />
           </el-form-item>

           </el-col>
             <el-col :span="8">
           <el-form-item label="有效期到" prop="validUntil">
             <el-date-picker v-model="chargeConfigFormData.validUntil" type="date" value-format="yyyy-MM-dd" style="width: 100%" placeholder="选择有效期开始日" clearable />
           </el-form-item>
           </el-col>
           </el-row>
           <el-col  v-if="chargeConfigFormData.frtNo === '配送费'" :span="8">
             <el-form-item label="物料名称：" prop="materialNo">
              <el-select v-model="chargeConfigFormData.materialNo" style="width: 100%;" clearable>
                <el-option v-for="option in options.materialOptions" :key="option.id" :label="option.materialName" :value="option.id" />
              </el-select>
            </el-form-item>
           </el-col>
            </el-row>
    <el-row :gutter="24" class="form-item-group">
      <el-col  v-if="chargeConfigFormData.frtNo === '配送费'" :span="8">
           <el-form-item label="计费类型" prop="inAndOutCharge">
             <el-radio-group v-model="chargeConfigFormData.inAndOutCharge">
               <el-radio v-for="option in $constant.inAndOutChargeOptions" :key="option.key" :label="option.key">{{ option.label }}</el-radio>
             </el-radio-group>
           </el-form-item>
           </el-col>
           <el-col  v-if="chargeConfigFormData.frtNo === '仓储费'" :span="8">
               <el-form-item label="开始数量" prop="startQuantity">
                 <el-input v-model="chargeConfigFormData.startQuantity" placeholder="请输入开始数量" />
               </el-form-item>
                </el-col>
                <el-col  v-if="chargeConfigFormData.frtNo === '仓储费'" :span="8">
                    <el-form-item label="最大数量" prop="endQuantity">
                      <el-input v-model="chargeConfigFormData.endQuantity" placeholder="请输入最大数量" />
                    </el-form-item>
                     </el-col>
            </el-row>
         <!--  <el-form-item label="转换单位" prop="convertQuantity">
             <el-input v-model="chargeConfigFormData.convertQuantity" placeholder="请输入转换单位" />
           </el-form-item> -->



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmChargeConfigFormDialog">确 定</el-button>
        <el-button @click="resetChargeConfigFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ChargeConfigApi from '@/api/pms/ChargeConfigApi'
import TWhWarehouseCustomerMaterialApi from '@/api/pms/TWhWarehouseCustomerMaterialApi'

const defaultChargeConfigFormData = {
  customerId: null,
  customerName: null,
  frtNo: null,
  frtName: null,
  settleBy: null,
  chargeTimeBy: null,
  amount: null,
  chargeType: null,
  customerType: null,
  inAndOutCharge: null,
  validUntil: null,
  convertQuantity: null,
  startQuantity: null,
  endQuantity: null,
  startDateFrom: null
}
export default {
  name: 'ChargeConfigList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions,
        materialOptions: []
      },
      queryParams: {
        frtName: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      chargeConfigFormDialog: {
        title: '',
        visible: false
      },
      chargeConfigFormData: Object.assign({}, defaultChargeConfigFormData),
      chargeConfigFormRules: {
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
         const params = {
           customerNo : this.chargeConfigFormData.customerId
         }
         TWhWarehouseCustomerMaterialApi.list(params).then(response => {
            this.options.materialOptions = response.data
          })
       },
    handleAdd() {
      this.openChargeConfigFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ChargeConfigApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openChargeConfigFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      ChargeConfigApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ChargeConfigApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openChargeConfigFormDialog(id) {
      this.chargeConfigFormDialog.visible = true
      if (id) {
        this.chargeConfigFormDialog.title = '仓储费率配置-修改'
        ChargeConfigApi.get(id).then(response => {
          this.chargeConfigFormData = {
            ...this.chargeConfigFormData,
            ...response.data
          }
        })
      } else {
        this.chargeConfigFormDialog.title = '仓储费率配置-添加'
      }
    },
    confirmChargeConfigFormDialog() {
      this.$refs['chargeConfigForm'].validate(valid => {
        if (valid) {
          if (this.chargeConfigFormData.id) {
            ChargeConfigApi.edit(this.chargeConfigFormData).then(response => {
              this.$message.success('修改成功')
              this.resetChargeConfigFormDialog()
              this.handleQuery()
            })
          } else {
            ChargeConfigApi.add(this.chargeConfigFormData).then(response => {
              this.$message.success('添加成功')
              this.resetChargeConfigFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetChargeConfigFormDialog() {
      this.chargeConfigFormDialog = {
        title: '',
        visible: false
      }
      this.chargeConfigFormData = Object.assign({}, defaultChargeConfigFormData)
      this.resetForm('chargeConfigForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
